package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.uikit.OnWheelChangedListener;
import com.lanlian.uikit.WheelView;
import com.lanlian.utillib.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/28 11:17
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class DateChooseDialog extends Dialog {

    private OnConfirmListener mOnConfirmListener;
    private Context mContext;

    private TextView mTvCancel;
    private TextView mTvConfirm;
    private WheelView mWheelYear;
    private WheelView mWheelMonth;
    private WheelView mWheelDay;

    private int mCurrYearIndex = 0;
    private int mCurrMonthIndex = 0;
    private int mCurrDayIndex = 0;

    private Calendar mSelectedCalendar;

    public DateChooseDialog(@NonNull Context context, OnConfirmListener onConfirmListener) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mOnConfirmListener = onConfirmListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date_choose);
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        initView();

        mSelectedCalendar = Calendar.getInstance();
        mWheelYear.setEntries(getYear());
        mWheelMonth.setEntries(getMonth());
        mWheelDay.setEntries(getDate(mSelectedCalendar.get(Calendar.MONTH)));
        mWheelYear.setCurrentIndex(mWheelYear.getItemSize() - 1);
        mWheelMonth.setCurrentIndex(mWheelMonth.getItemSize() - 1);
        mWheelDay.setCurrentIndex(mWheelDay.getItemSize() - 1);
        mSelectedCalendar.setTime(new Date());
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

    private void initView() {
        mTvCancel = findViewById(R.id.tv_cancel);
        mTvConfirm = findViewById(R.id.tv_ok);
        mWheelDay = findViewById(R.id.wheel_day);

        mWheelYear = findViewById(R.id.wheel_year);
        mWheelMonth = findViewById(R.id.wheel_month);
        mWheelDay = findViewById(R.id.wheel_day);

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSelectedCalendar.set(Calendar.MONTH, mCurrMonthIndex);
                mSelectedCalendar.set(Calendar.DAY_OF_MONTH, mCurrDayIndex + 1);
                long time = mSelectedCalendar.getTimeInMillis();
                mOnConfirmListener.onConfirmClick(DateUtils.longToString(time, "yyyy-MM-dd"), time);
                dismiss();
            }
        });
        mWheelYear.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView view, int oldIndex, int newIndex) {
                if (mCurrYearIndex != newIndex) {
                    mCurrYearIndex = newIndex;
                    String selectYear = (String) mWheelYear.getCurrentItem();
                    if (TextUtils.isEmpty(selectYear)) {
                        return;
                    }
                    mSelectedCalendar.set(Calendar.YEAR, Integer.parseInt(selectYear.replace("年", "")));
                    mWheelMonth.entriesNotify(getMonth());
                    mWheelDay.entriesNotify(getDate(mCurrMonthIndex));
                }
            }
        });
        mWheelMonth.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView view, int oldIndex, int newIndex) {
                if (mCurrMonthIndex != newIndex) {
                    mCurrMonthIndex = newIndex;
                    String selectMonth = (String) mWheelMonth.getCurrentItem();
                    if (TextUtils.isEmpty(selectMonth)) {
                        return;
                    }
                    mSelectedCalendar.set(Calendar.MONTH, mCurrMonthIndex);
                    mSelectedCalendar.set(Calendar.DAY_OF_MONTH, mCurrDayIndex + 1);
                    mWheelDay.entriesNotify(getDate(mCurrMonthIndex));
                }
            }
        });
        mWheelDay.setOnWheelChangedListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView view, int oldIndex, int newIndex) {
                if (mCurrDayIndex != newIndex) {
                    mCurrDayIndex = newIndex;
                    String selectDay = (String) mWheelDay.getCurrentItem();
                    if (TextUtils.isEmpty(selectDay)) {
                        return;
                    }
                    mSelectedCalendar.set(Calendar.DAY_OF_MONTH, mCurrDayIndex + 1);
                }
            }
        });
    }

    public interface OnConfirmListener {
        void onConfirmClick(String timeStr, long time);
    }

    private ArrayList<String> getDate(int month) {
        ArrayList<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();

        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.YEAR, mSelectedCalendar.get(Calendar.YEAR));
        calendar2.set(Calendar.MONTH, month);
        int days = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);

        if (calendar.get(Calendar.YEAR) == mSelectedCalendar.get(Calendar.YEAR) &&
                calendar.get(Calendar.MONTH) == month) {
            days = calendar.get(Calendar.DAY_OF_MONTH);
        }
        for (int i = 1; i <= days; i++) {
            list.add(String.format("%02d日", i));
        }
        return list;
    }

    private ArrayList<String> getMonth() {
        ArrayList<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int month = 12;
        if (calendar.get(Calendar.YEAR) == mSelectedCalendar.get(Calendar.YEAR)) {
            month = calendar.get(Calendar.MONTH) + 1;
        }
        for (int i = 1; i <= month; i++) {
            list.add(String.format("%02d月", i));
        }
        return list;
    }

    private ArrayList<String> getYear() {
        ArrayList<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        for (int i = 2017; i <= year; i++) {
            list.add(String.format("%04d年", i));
        }
        return list;
    }


}
